package me.lwn.auth.resources;

import me.lwn.auth.application.UserManager;
import me.lwn.auth.common.HttpMsg;
import me.lwn.auth.common.PageRequest;
import me.lwn.auth.common.PageResponse;
import me.lwn.auth.domain.User;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    private final UserManager userManager;

    public UserController(UserManager userManager) {
        this.userManager = userManager;
    }

    /**
     * 新增用户信息
     */
    @PostMapping
    public HttpMsg<String> add(@RequestBody User user) {
        userManager.add(user);
        return HttpMsg.success("新增用户成功");
    }

    /**
     * 修改用户信息
     */
    @PutMapping
    public HttpMsg<String> update(@RequestBody User user) {
        userManager.update(user);
        return HttpMsg.success("修改用户成功");
    }

    /**
     * 查询注册用户数
     */
    @GetMapping("regis-num")
    public HttpMsg<Long> registerNumber() {

        HttpMsg<Long> response = HttpMsg.success("查询用户注册数成功");
        response.setData(userManager.registerNumber());
        return response;
    }

    /**
     * 根据用户username获取用户信息
     *
     * @param username 账号
     */
    @GetMapping("info/{username}")
    public User info(@PathVariable String username) {
        return userManager.getByUsername(username);
    }

    /**
     * 根据id获取用户信息
     *
     * @param id 用户表主键
     */
    @GetMapping("{id}")
    public User view(@PathVariable String id) {
        return userManager.get(id);
    }

    /**
     * 用户分页列表
     */
    @PostMapping("page")
    public PageResponse<User> page(@RequestBody PageRequest params) {
        return userManager.page(params);
    }
}